package com.ruoyi.store.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.store.service.IStockLossService;
import com.ruoyi.system.api.domain.store.StockLoss;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 报损单Controller
 *
 * @author wl
 * @date 2024-11-04
 */
@RestController
@RequestMapping("/stockLoss")
@Slf4j
public class StockLossController extends BaseController {
    @Autowired
    private IStockLossService stockLossService;

    /**
     * 查询报损单列表
     */
    @RequiresPermissions("store:stockLoss:list")
    @GetMapping("/list")
    public TableDataInfo list(StockLoss stockLoss) {
        log.info("StockLossController list stockLoss:{}", stockLoss);
        List<StockLoss> list = stockLossService.selectStockLossList(stockLoss);
        TableDataInfo tableDataInfo = getDataTable(list);
        log.info("StockLossController list tableDataInfo:{}", tableDataInfo);
        return tableDataInfo;
    }

    /**
     * 导出报损单列表
     */
    @RequiresPermissions("store:stockLoss:export")
    @Log(title = "报损单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StockLoss stockLoss) {
        List<StockLoss> list = stockLossService.selectStockLossList(stockLoss);
        ExcelUtil<StockLoss> util = new ExcelUtil<StockLoss>(StockLoss.class);
        util.exportExcel(response, list, "报损单数据");
    }

    /**
     * 获取报损单详细信息
     */
    @RequiresPermissions("store:stockLoss:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        log.info("StockLossController getInfo id:{}", id);
        return success(stockLossService.selectStockLossById(id));
    }

    /**
     * 新增报损单
     */
    @RequiresPermissions("store:stockLoss:add")
    @Log(title = "报损单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StockLoss stockLoss) {
        log.info("StockLossController add stockLoss:{}", stockLoss);
        AjaxResult ajaxResult = stockLossService.insertStockLoss(stockLoss);
        log.info("StockLossController add ajaxResult:{}", ajaxResult);
        return ajaxResult;
    }

    /**
     * 修改报损单
     */
    @RequiresPermissions("store:stockLoss:edit")
    @Log(title = "报损单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StockLoss stockLoss) {
        log.info("StockLossController edit stockLoss:{}", stockLoss);
        AjaxResult ajaxResult = stockLossService.updateStockLoss(stockLoss);
        log.info("StockLossController edit ajaxResult:{}", ajaxResult);
        return ajaxResult;
    }

    /**
     * 删除报损单
     */
    @RequiresPermissions("store:stockLoss:remove")
    @Log(title = "报损单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        log.info("StockLossController remove ids:{}", Arrays.toString(ids));
        AjaxResult ajaxResult = stockLossService.deleteStockLossByIds(ids);
        log.info("StockLossController remove ajaxResult:{}", ajaxResult);
        return ajaxResult;
    }

    @GetMapping("/check/orderCode/{orderCode}")
    public AjaxResult checkOrderCode(@PathVariable String orderCode) {
        log.info("StockLossController checkOrderCode orderCode:{}", orderCode);
        AjaxResult ajaxResult = stockLossService.checkOrderCode(orderCode);
        log.info("StockLossController checkOrderCode ajaxResult:{}", ajaxResult);
        return ajaxResult;
    }
}
